app.controller('vmUpdateVmwareCtrl', ['$rootScope', '$scope', '$state', 'httpLoad', '$stateParams', '$modal', function ($rootScope, $scope, $state, httpLoad, $stateParams, $modal) {
	$rootScope.link = '/statics/css/user.css';
	$scope.param = {
		rows: 10
	};
	$scope.showDetail = 1;
	$scope.showVmware = 1;
	var resourcetabLocation1 = sessionStorage.getItem('resourcetabLocation1');
	if(resourcetabLocation1&&resourcetabLocation1.indexOf("virtualUpdate")>=0) $rootScope.moduleTitle = '资源管理 > 主机资源';
	else $rootScope.moduleTitle = '平台管理 > VCenter';

	$scope.goBack = function () {
		if(resourcetabLocation1&&resourcetabLocation1.indexOf("virtualUpdate")>=0){
			$state.go('app.assets.resource');
			sessionStorage.setItem('resourcetabLocation','virtualUpdate');
		}
		else {
			$state.go('app.config.vmwareTab',{id:$stateParams.id});
			sessionStorage.setItem('vmTabvendor', JSON.stringify({id:$stateParams.id}));
		}
	};
	(function () {
		$scope.addData = {};
		$scope.param = {};
		$scope.isShowTemplate = true;
		var id = $stateParams.id;
		httpLoad.loadData({
			url: '/vm/detail',
			method: 'GET',
			data: {id: id},
			success: function (data) {
				if (data.success && data.data) {
					$scope.addData = data.data;
					$scope.addData.datastore = '';
					$scope.selectNetworkListData = data.data.netcards;
					$scope.selectNetworkListData.forEach(function (item) {
						item.operation = 'NORMAL';
						item.cacheOperation = 'NORMAL';
						item.disLabel = item.label.replace(/Network adapter/g,'网络适配器');
					});
					getDatastore();
				}
			}
		});
		//获取存储
		var getDatastore = function () {
			httpLoad.loadData({
				url:'/datastore/list',
				method: 'POST',
				data:{
					simple:true,
					params:JSON.stringify([{"param":{"vendorId":$scope.addData.vendorId,"vhostId":$scope.addData.id,category:'VM'},"sign":"EQ"}])
				},
				noParam: true,
				success:function(data){
					if(data.success){
						$scope.datastoreList = data.data.rows;
					}
				}
			});
		};
	
		$scope.check = function (action, value, min, max) {
			var number = parseInt(value);
			if (action == 0) var name = 'CPU' + '范围是' + min + '-' + max + ',' + '请重新输入';
			if (action == 1) var name = '内存' + '范围是' + min + '-' + max + ',' + '请重新输入';
			if (action == 2) var name = '磁盘' + '范围是' + min + '-' + max + ',' + '请重新输入';
			if (value < min || value > max) {
				$scope.pop(name, 'error');
				return;
			}
		};
		$scope.checkDisk = function (flag) {
			if(flag) {
				for(var i = 0; i < $scope.datastoreList.length; i++){
					var item = $scope.datastoreList[i];
					if(item.name == $scope.addData.datastore) {
						$scope.freeSpace = item.value;
						break;
					};
				}
			};
			if($scope.addData.datastore && $scope.addData.extendDisk){
			if($scope.freeSpace/1 < $scope.addData.extendDisk/1 ) $scope.addData.extendDisk = $scope.freeSpace;
			}
		};
		$scope.operationStatus = {
			REMOVE:'删除中',
			ADD:'新增中',
			EDIT:'编辑中'
		};
		$scope.recoveryNetwork = function (row) {
			row.operation = row.cacheOperation;
		};
		$scope.delNetwork = function (row,index) {
			if(!row.cacheOperation){
				$scope.selectNetworkListData.splice(index,1);
			}else{
				row.cacheOperation = row.operation;
				row.operation = 'REMOVE';
			}
		};
		$scope.operateNetwork = function (row,index) {
			var modalInstance = $modal.open({
				templateUrl: '/statics/tpl/config/vmware/vm/addNetworkModal.html',
				controller: 'operateNetworkModalCtrl',
				backdrop: 'static',
				resolve: {
					id: function () {
						return $scope.addData.hostId
					},
					itemData:function () {
						return row
					}
				}
			});
			modalInstance.result.then(function (data) {
				if(row){
					$scope.selectNetworkListData[index] = data;
				}else{
					$scope.selectNetworkListData.push(data);
				}
			});
		};
		var formatNetworks = function () {
			var toObjFormat = function(obj) {
				for (var a in obj) {
					if (obj[a] === "") delete obj[a];
				}
				return obj;
			}
			var arr = [];
			$scope.selectNetworkListData.forEach(function (item) {
				if(item.operation != 'NORMAL'){
					var obj = toObjFormat({
						name:item.name,
						type:item.type,
						label:item.label,
						operation:item.operation
					});
					arr.push(obj);
				}
			});
			return arr;
		}
		$scope.ok = function () {
			$scope.addData.operations = formatNetworks();
			httpLoad.loadData({
				url: '/vm/modify',
				method: 'POST',
				data: $scope.addData,
				success: function (data) {
					if (data.success) {
						$scope.pop(data.message);
						$scope.goBack();
					}
				}
			});
		};
		$scope.cancel = function () {
			$scope.goBack();
		};
	})();
}]);
